/* ****************************************************************************
scenenode.h
-------------------------------------------------------------------------------

Copyright (c) 2017, Tain L.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**************************************************************************** */

#if !defined(SPEARS_SPEARS_SCENENODE_H)
#define SPEARS_SPEARS_SCENENODE_H

#include "../my-prerequisites.h"
#include "../../mathinc/mathinc.h"
#include "../aggregates/pool.h"
#include "transform.h"
#include "nodecom.h"

namespace Spears
{
	// ------------------------------------------------------------------------
	// declaration
	// ------------------------------------------------------------------------

	class Scene;		// forward declaration.

	class SceneNode
	{
		friend class Scene;
	public:
		SceneNode();
		SceneNode(const SceneNode& rhs);
		SceneNode(SceneNode&& rhs);
		virtual ~SceneNode();

		SceneNode& operator=(const SceneNode& rhs);
		SceneNode& operator=(SceneNode&& rhs);

	public:
		void add_child(const Pool<SceneNode>::Addr& address);
		void add_component(const Pool<NodeComponent>::Addr& address);
		Transform& get_transform();
		const std::vector<Pool<SceneNode>::Addr>& get_children() const;
		const std::vector<Pool<NodeComponent>::Addr>& get_components() const;
		bool is_detectable() const;
		
	protected:
		Math::AABBox							m_bbox;
		Transform								m_transform;
		std::vector<Pool<SceneNode>::Addr>		m_children;
		std::vector<Pool<NodeComponent>::Addr>	m_coms;
		
	protected:
		bool									m_detectable;
	}; // class SceneNode
	
	// ------------------------------------------------------------------------
	// implementation
	// ------------------------------------------------------------------------
	
	inline SceneNode::SceneNode()
		: m_detectable(false)
	{}

	inline SceneNode::SceneNode(const SceneNode& rhs)
	{
		m_children = rhs.m_children;
		m_coms = rhs.m_coms;
	}

	inline SceneNode::SceneNode(SceneNode&& rhs)
	{
		std::swap(*this, rhs);
		rhs.m_coms.clear();
	}

	inline SceneNode::~SceneNode()
	{}

	inline SceneNode& SceneNode::operator=(const SceneNode& rhs)
	{
		m_children = rhs.m_children;
		m_coms = rhs.m_coms;
	}

	inline SceneNode& SceneNode::operator=(SceneNode&& rhs)
	{
		std::swap(*this, rhs);
		rhs.m_children.clear();
	}

	inline void SceneNode::add_child(const Pool<SceneNode>::Addr& address)
	{
		m_children.push_back(address);
	}

	inline void SceneNode::add_component(const Pool<NodeComponent>::Addr& address)
	{
		assert(address.is_valid());
		if (address.is_valid())
		{
			m_coms.push_back(address);
			m_detectable |= address.get_ref().is_detectable();
		}
	}

	inline const std::vector<Pool<SceneNode>::Addr>& SceneNode::get_children() const
	{
		return m_children;
	}

	inline const std::vector<Pool<NodeComponent>::Addr>& SceneNode::get_components() const
	{
		return m_coms;
	}

	inline Transform& SceneNode::get_transform()
	{
		return m_transform;
	}

	inline bool SceneNode::is_detectable() const
	{
		return m_detectable;
	}
}

#endif // SPEARS_SPEARS_SCENENODE_H